﻿#pragma once

#include <qobject.h>
#include <qvector.h>
#include <qtreewidget.h>

#include "TurnTableCodec.h"

class ScriptInstruction;

class ScriptInstructionFactory : public QObject
{
  Q_OBJECT

public:
  static const QString itemText_separator_head;
  static const QString itemText_separator_arg;
  static const QString itemText_wait;
  static const QString itemText_wait_duration;
  static const QString itemText_loop;
  static const QString itemText_loop_index;
  static const QString itemText_loop_count;
  static const QString itemText_startCollection_dataReader;
  static const QString itemText_stopCollection_dataReader;
  static const QString itemText_connectToTurnTable;
  static const QString itemText_disconnectFromTurnTable;
  static const QString ItemText_setTurnTableLoaded;
  static const QString ItemText_setTurnTableUnloaded;
  static const QString itemText_powerOnTurnTable;
  static const QString itemText_powerOffTurnTable;
  static const QString itemText_closeTurnTableCircuit;
  static const QString itemText_openTurnTableCircuit;
  static const QString itemText_stopTurnTable;
  static const QString itemText_emergencyStopTurnTable;
  static const QString itemText_turnTableSearchZero;
  static const QString itemText_turnTableMoveToZero;
  static const QString itemText_setTurnTablePositionMode;
  static const QString itemText_setTurnTablePositionMode_position;
  static const QString itemText_setTurnTablePositionMode_rate;
  static const QString itemText_setTurnTablePositionMode_acceleration;
  static const QString itemText_setTurnTableRateMode;
  static const QString itemText_setTurnTableRateMode_rate;
  static const QString itemText_setTurnTableRateMode_acceleration;
  static const QString itemText_turnTableRunPositionMode;
  static const QString itemText_turnTableRunRateMode;
  static const QString itemText_searchTurnTableCurrentState;
  static const QString itemText_searchTurnTableSearchZeroState;
  static const QString itemText_searchTurnTableRunningState;
  static const QString itemText_searchTurnTableRunningMode;
  static const QString itemText_connectToThermostat;
  static const QString itemText_disconnectFromThermostat;
  static const QString itemText_setThermostatTargetTemperature;
  static const QString itemText_setThermostatTargetTemperature_temperature;
  static const QString itemText_searchThermostatCurrentTemperature;
  static const QString itemText_setThermostatTemperatureRate;
  static const QString itemText_setThermostatTemperatureRate_rate;
  static const QString itemText_quitTurnTableConsole;
public:
  ScriptInstructionFactory(QObject* parent = Q_NULLPTR);
  ~ScriptInstructionFactory();

  //其他格式转使用格式
public:
  //如果调用后isOk的结果为true则意味着格式转换成功，结果为false则意味着格式转换失败
  static QVector<QVector<ScriptInstruction*>> floatListToScriptList(QVector<QVector<QVector<float>>> floatList, bool* isOk);
  //如果调用后isOk的结果为true则意味着格式转换成功，结果为false则意味着格式转换失败
  static QVector<QTreeWidgetItem*> floatListToTreeWidgetItemList(QVector<QVector<QVector<float>>> floatList, bool* isOk);
  //如果调用后isOk的结果为true则意味着格式转换成功，结果为false则意味着格式转换失败
  static QVector<QVector<QVector<float>>> scriptListToFloatList(QVector<QVector<ScriptInstruction*>> scriptList, bool* isOk);
  //如果调用后isOk的结果为true则意味着格式转换成功，结果为false则意味着格式转换失败
  static QVector<QTreeWidgetItem*> scriptListToTreeWidgetItemList(QVector<QVector<ScriptInstruction*>> scriptList, bool* isOk);
  //如果调用后isOk的结果为true则意味着格式转换成功，结果为false则意味着格式转换失败
  static QVector<QVector<QVector<float>>> treeWidgetItemListToFloatList(QVector<QTreeWidgetItem*> treeWidgetItemList, bool* isOk);
  //如果调用后isOk的结果为true则意味着格式转换成功，结果为false则意味着格式转换失败
  static QVector<QVector<ScriptInstruction*>> treeWidgetItemListToScriptList(QVector<QTreeWidgetItem*> treeWidgetItemList, bool* isOk);
private:
  static ScriptInstruction* floatToScript(QVector<float> scriptInstruction);
  static QTreeWidgetItem* floatToTreeWidgetItem(QVector<float> scriptInstruction, QVector<QVector<QVector<float>>> floatList);
  static QVector<float> treeWidgetItemToFloat(QTreeWidgetItem* scriptInstruction, QVector<QTreeWidgetItem*> parentTreeWidgetItemList);

public://制作脚本指令描述
  static QString getDescribe_Wait(int milliseconds);
  static QString getDescribe_Loop(int scriptIndex, int loopCount);
  static QString getDescribe_Loop(int loopCount);
  static QString getDescribe_DataReader_StartCollection();
  static QString getDescribe_DataReader_StopCollection();
  static QString getDescribe_TurnTable_Connect();
  static QString getDescribe_TurnTable_Disconnect();
  static QString getDescribe_TurnTable_SetLoad(TurnTableCodec::TurnTableLoadMode loadMode);
  static QString getDescribe_TurnTable_PowerOn();
  static QString getDescribe_TurnTable_PowerOff();
  static QString getDescribe_TurnTable_CloseCircuit();
  static QString getDescribe_TurnTable_OpenCircuit();
  static QString getDescribe_TurnTable_Stop();
  static QString getDescribe_TurnTable_EmergencyStop();
  static QString getDescribe_TurnTable_PositionMode(float position, float rate, float acceleration);
  static QString getDescribe_TurnTable_RateMode(float rate, float acceleration);
  static QString getDescribe_TurnTable_Run(TurnTableCodec::TurnTableRunningMode runningMode);
  static QString getDescribe_Thermostat_Connect();
  static QString getDescribe_Thermostat_Disconnect();
  static QString getDescribe_Thermostat_TargetTemperature(float targetTemperature);
  static QString getDescribe_Thermostat_TemperatureRate(float targetTempratureRate);
public:
  static QString getDescribeHead(QString scriptDescribe);
  static QVector<float> getDescribeArgList(QString scriptDescribe);
public:
  //按脚本执行顺序搜索含有子序列的TreeWidgetItem
  static QVector<QTreeWidgetItem*> getParentTreeWidgetItemList(QVector<QTreeWidgetItem*> treeWidgetItemList);
private:
  static void circularDelete(QTreeWidgetItem* treeWidgetItem);
};

